package com.appspot.etao.service;

import java.util.ArrayList;
import java.util.List;

import com.appspot.etao.Constant;
import com.appspot.etao.model.ShopModel;
import com.taobao.top.TopJsonRestClient;
import com.taobao.top.domain.ItemCat;
import com.taobao.top.domain.ResponseList;
import com.taobao.top.domain.Shop;
import com.taobao.top.domain.ShopCat;
import com.taobao.top.domain.TaobaokeItem;
import com.taobao.top.domain.TaobaokeShop;
import com.taobao.top.domain.User;
import com.taobao.top.request.ItemCatsGetRequest;
import com.taobao.top.request.ShopCatsGetRequest;
import com.taobao.top.request.ShopGetRequest;
import com.taobao.top.request.TaobaokeItemsGetRequest;
import com.taobao.top.request.TaobaokeShopsConvertRequest;
import com.taobao.top.request.UserGetRequest;

/**
 * 获取Top平台数据
 * 
 * @author Administrator
 *
 */
public class TopService extends BaseService {

	/**
	 * 获取前台展示的店铺类目
	 */
	public List<ShopCat> getShopCatList() {
		TopJsonRestClient client = ClientFactory.getJsonClient();
		
		ShopCatsGetRequest request = new ShopCatsGetRequest();
		ResponseList<ShopCat> responseShopCatList = client.getShopCats(request);
		
		List<ShopCat> shopCatList = responseShopCatList.getContent();
		
		if (shopCatList == null) shopCatList = new ArrayList<ShopCat>();
		
		return shopCatList;
	}

	/**
	 * 获取所有淘宝客推荐商品的类目，递归调用
	 * @return
	 */
	public List<ItemCat> getItemCatList() {
		TopJsonRestClient client = ClientFactory.getJsonClient();
		
		ItemCatsGetRequest request = new ItemCatsGetRequest();
		request.setFields(Constant.FIELDS_ITEMCAT);
		request.setParentCid("0");
		ResponseList<ItemCat> responseItemCatList = client.getItemCats(request);
		
		List<ItemCat> itemCatList = new ArrayList<ItemCat>();
		
		itemCatList.addAll(getItemCatList(responseItemCatList.getContent()));
		
		if (itemCatList == null) itemCatList = new ArrayList<ItemCat>();
		
		return itemCatList;
	}
	
	/**
	 * 递归查找所有子节点
	 * @param criteriaList
	 * @return
	 */
	private List<ItemCat> getItemCatList(List<ItemCat> criteriaList) {
		List<ItemCat> itemCatList = new ArrayList<ItemCat>();
		
		TopJsonRestClient client = ClientFactory.getJsonClient();
		
		for (ItemCat itemCat : criteriaList) {
			if (itemCat.isParent()) {//如果是根节点，递归调用，找出所有子节点
				ItemCatsGetRequest request = new ItemCatsGetRequest();
				request.setFields(Constant.FIELDS_ITEMCAT);
				request.setParentCid(itemCat.getCid().toString());
				
				ResponseList<ItemCat> responseItemCatList = client.getItemCats(request);
				itemCatList.addAll(getItemCatList(responseItemCatList.getContent()));
			} else {//子节点，直接保存
				itemCatList.add(itemCat);
			}
		}
		
		if (itemCatList == null) itemCatList = new ArrayList<ItemCat>();
		
		return itemCatList;
	}
	
	/**
	 * 获取所有TaobaokeItem数据
	 * @param taobaokeRequest
	 * @return
	 */
	public List<TaobaokeItem> getAllTaobaokeItemList(TaobaokeItemsGetRequest taobaokeRequest) {
		TopJsonRestClient client = ClientFactory.getJsonClient();
		taobaokeRequest.setFields(Constant.FIELDS_TAOBAOKEITEM);
		taobaokeRequest.setNick(Constant.NICK);
		ResponseList<TaobaokeItem> taobaokeItemResponseList = client.getTaobaokeItems(taobaokeRequest);
		
		List<TaobaokeItem> taobaokeItemList = taobaokeItemResponseList.getContent();
		
		if (taobaokeItemList == null) taobaokeItemList = new ArrayList<TaobaokeItem>();
		
		return taobaokeItemList;
	}
	
	/**
	 * 过滤掉所有TaobaokeItem数据中nick重复的数据
	 * @param allTaobaokeItemList
	 * @return
	 */
	public List<TaobaokeItem> getDistinctTaobaokeItemList(List<TaobaokeItem> allTaobaokeItemList) {
		List<TaobaokeItem> distinctItemList = new ArrayList<TaobaokeItem>();
		
		List<String> distinctNickList = new ArrayList<String>();
		for (TaobaokeItem taobaokeItem : allTaobaokeItemList) {
			String nick = taobaokeItem.getNick();
			if (!distinctNickList.contains(nick)) {
				distinctNickList.add(nick);
				
				distinctItemList.add(taobaokeItem);
			}
		}
		
		if (distinctItemList == null) distinctItemList = new ArrayList<TaobaokeItem>();
		
		return distinctItemList;
	}
	
	/**
	 * 获取TaobaokeItem数据中的所有商铺信息
	 * @param distinctTaobaokeItemList
	 * @return
	 */
	public List<Shop> getShopList(List<TaobaokeItem> distinctTaobaokeItemList) {
		List<Shop> shopList = new ArrayList<Shop>();
		
		TopJsonRestClient client = ClientFactory.getJsonClient();
		for (TaobaokeItem taobaokeItem : distinctTaobaokeItemList) {
			ShopGetRequest shopRequest = new ShopGetRequest();
			shopRequest.setFields(Constant.FIELDS_SHOP);
			shopRequest.setNick(taobaokeItem.getNick());
			Shop shop = client.getShop(shopRequest);
			
			shopList.add(shop);
		}
		
		if (shopList == null) shopList = new ArrayList<Shop>();
		
		return shopList;
	}
	
	/**
	 * 获取TaobaokeItem数据中的所有商铺信息(TaobaokeShop)
	 * @param distinctTaobaokeItemList
	 * @return
	 */
	public List<TaobaokeShop> getTaobaokeShopList(List<ShopModel> shopList) {
		List<TaobaokeShop> taobaokeShopList = new ArrayList<TaobaokeShop>();
		
		TopJsonRestClient client = ClientFactory.getJsonClient();
		for (ShopModel shop : shopList) {
			TaobaokeShopsConvertRequest shopConvertRequest = new TaobaokeShopsConvertRequest();
			shopConvertRequest.setFields(Constant.FIELDS_SHOP_TAOBAOKE);
			shopConvertRequest.setNick(Constant.NICK);
			shopConvertRequest.setSids(shop.getSid());
			ResponseList<TaobaokeShop> responseTaobaokeShopList = client.convertTaobaokeShops(shopConvertRequest);
			
			taobaokeShopList.add(responseTaobaokeShopList.getContent().get(0));
		}
		
		if (taobaokeShopList == null) taobaokeShopList = new ArrayList<TaobaokeShop>();
		
		return taobaokeShopList;
	}
	/**
	 * 获取TaobaokeItem数据中的所有用户信息
	 * @param distinctTaobaokeItemList
	 * @return
	 */
	public List<User> getUserList(List<TaobaokeItem> distinctTaobaokeItemList) {
		List<User> userList = new ArrayList<User>();
		
		TopJsonRestClient client = ClientFactory.getJsonClient();
		for (TaobaokeItem taobaokeItem : distinctTaobaokeItemList) {
			UserGetRequest userRequest = new UserGetRequest();
			userRequest.setFields(Constant.FIELDS_USER);
			userRequest.setNick(taobaokeItem.getNick());
			User user = client.getUser(userRequest);
			
			userList.add(user);
		}
		
		if (userList == null) userList = new ArrayList<User>();
		
		return userList;
	}
	
	
}
